// index.js
// 获取应用实例
const app = getApp()
const util=require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  order:function(){
    
    let that=this
    let orderList=JSON.parse(decodeURIComponent(that.options.list))
      console.log('点餐列表',orderList)
      let tableId=that.options.table_id
      console.log('桌号',tableId)
      let priceNum=0
      let priceNum1=0
      for(let i=0;i<orderList.length;i++){
        if(orderList[i].menu_id==1){
          orderList[i].vegetable_price=that.options.special_offer*orderList[i].vegetable_price
          console.log(orderList[i].vegetable_price)
      }
      else{
        orderList[i].vegetable_price= orderList[i].vegetable_price
      } 
      priceNum+= orderList[i].num*orderList[i].vegetable_price
      priceNum1+= orderList[i].num*orderList[i].vegetable_price
      if(priceNum1>=that.options.max){
        priceNum=priceNum1-that.options.min
     }   
       
      }
      if(wx.getStorageSync('access_token')){
        wx.request({
          method: "post",
          dataType: "json",
          url: 'https://999.tianba.work/api/Order/add',
          data:{
            session3rd:wx.getStorageSync('access_token'),
            table_id:tableId,
            orderList:orderList,
            priceNum:priceNum,
            priceNum1:priceNum1
          },
          success(res){
            console.log(res)
            // wx.removeStorageSync('access_token')
            wx.switchTab({ 
              url: '/pages/index/index',
            })
            setTimeout(() => {
              wx.showToast({
                title: '下单成功',
                icon: 'success',
                duration: 1500
              })
            }, 1000); 
          }       
        })
      }   
  },
  onLoad: function (options) {
    // console.log('再来一单对应id',options.order_id)
    let that=this
    wx.request({
      url: 'https://999.tianba.work/api/status/index',
      success(res){
        console.log('状态',res)
        that.setData({
          status:res.data.data.status,
          })
      }
    })
    let order=JSON.parse(decodeURIComponent(options.list))
        console.log(11,order)
       
        let priceNum=0
        for(let i=0;i<order.length;i++){    
          if(order[i].menu_id==1){
            order[i].vegetable_price=options.special_offer*order[i].vegetable_price
            console.log(order[i].vegetable_price)
        }
        else{
          order[i].vegetable_price= order[i].vegetable_price
        }    
          priceNum+=order[i].vegetable_price*order[i].num
       }
       if(priceNum>=options.max){
          priceNum-=options.min
       }
       that.setData({
          table:options.table_id,
          order:order,
          priceNum:priceNum.toFixed(2),
          time:util.formatTime(new Date())
        })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
